package com.fas.hehe.config.controller;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.fas.core.BaseController;
import com.fas.core.constant.R;
import com.fas.core.plugin.jfinal.route.ControllerBind;
import com.fas.hehe.config.model.DictionaryData;
import com.fas.hehe.config.service.DictionaryDataService;
import com.jfinal.aop.Before;
import com.jfinal.plugin.ehcache.CacheName;
import com.jfinal.plugin.ehcache.EvictInterceptor;

/**
 * 
 * 字典数据管理
 *
 * @Copyright: Copyright (c) 2016 FAS
 * @anthor yixiaoping<thinker1013@163.com>
 * @since 2016年1月23日 下午1:03:53
 * @version V1.0
 */
@RequiresAuthentication
@ControllerBind(controllerKey = "/config/dictionaryData", viewPath = "/config/dictionaryData")
public class DictionaryDataController extends BaseController {

	public DictionaryDataService dictionaryDataService;

	// 修改字典数据优先级别(显示顺序 )
	@RequiresPermissions(value = { R.Authority.Dict.UPDATE }, logical = Logical.AND)
	@Before(EvictInterceptor.class)
	@CacheName(R.CacheName.GLOBAL_DICT_DATA)
	public void changeDictionaryDataPriority() {
		String dictCode = this.getPara("dictCode");
		String type = this.getPara("type");
		int orderby = this.getParaToInt("priority");
		boolean isSuccess = dictionaryDataService.changeDictionaryDataPriority(dictCode, type, orderby);
		this.returnJsonResult(isSuccess);
	}

	// 删除字典数据
	@RequiresPermissions(value = { R.Authority.Dict.DELETE }, logical = Logical.AND)
	@Before(EvictInterceptor.class)
	@CacheName(R.CacheName.GLOBAL_DICT_DATA)
	public void deleteDictionaryData() {
		boolean isSuccess = dictionaryDataService.deleteDictionaryDataById(this.getParaToLong("id"));
		this.returnJsonResult(isSuccess);
	}

	// 保存字典数据(for create OR update)
	@RequiresPermissions(value = { R.Authority.Dict.CREATE, R.Authority.Dict.UPDATE }, logical = Logical.OR)
	@Before(EvictInterceptor.class)
	@CacheName(R.CacheName.GLOBAL_DICT_DATA)
	public void saveDictionaryData() {
		DictionaryData dictionaryData = (DictionaryData) this.getModel(DictionaryData.class, "dictionaryData");
		boolean isSuccess = dictionaryDataService.saveDictionaryData(dictionaryData);
		this.returnJsonResult(isSuccess);
	}

	// 根据ID查询字典数据
	@RequiresPermissions(value = { R.Authority.Dict.QUERY }, logical = Logical.AND)
	public void findDictionaryDataById() {
		DictionaryData dictionaryData = dictionaryDataService.findDictionaryDataById(this.getParaToLong("id"));
		this.returnJsonResult(dictionaryData);
	}

}
